package com.example.tiku05;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.example.tiku05.View.NetView;
import com.example.tiku05.adapter.VpFragmentAdapter;
import com.example.tiku05.bean.ProjectTypeBean;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements NetView {

    private TabLayout tab;
    private ViewPager vp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        tab = (TabLayout) findViewById(R.id.tab);
        vp = (ViewPager) findViewById(R.id.vp);
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setTypeData(List<ProjectTypeBean.DataBean> types) {
        //通过循环types为vp创建26个pager页面
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < types.size(); i++) {
            VpFragment vpFragment = new VpFragment();
            Bundle b = new Bundle();
            b.putInt("cid", types.get(i).getId());
            vpFragment.setArguments(b);
            fragments.add(vpFragment);
        }
        //循环结束，得到最终集合 26个fragment对象,配置给vp
        VpFragmentAdapter adapter = new VpFragmentAdapter(getSupportFragmentManager(), fragments, types);
        vp.setAdapter(adapter);
        tab.setupWithViewPager(vp);//tablayout和vp结合
    }
}
